// const axios = require('axios')
const lrz = require('lrz')
import Vue from 'vue'

// Vue.axios.defaults.withCredentials = "include";
// Vue.axios.defaults.withCredentials = "include";

// var base = "http://localhost:8089"
var base = ""

switch (process.env.NODE_ENV) {
    case 'development':
        base = "http://127.0.0.1:5000";
        break;
    case 'production':
        base = ""
        break;
    default:
        break;
}

export const SCREENSHOTS_API = base + "/screen_shots"
export const DEB_API = base + "/deb_file"
export const SCREENSHOTS_SOURCE = base + "/images"

export function getCategoryList() {
    return new Promise((resolve, reject) => {
        Vue.axios.get(base + "/categorys")
            .then(res => {
                var list = JSON.parse(res["data"]["msg"])
                resolve(list)
            })
            .catch(error => reject(error))
    })
}

export function getDebListByCategory(categoryId, page) {
    return new Promise((resolve, reject) => {
        Vue.axios.get(`${base}/debs/category/${categoryId}/page/${page}`)
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}

export function importAllDeb() {
    var categorys = [
        { "name": "games", "id": 18 },
        { "name": "music", "id": 19 },
        { "name": "network", "id": 20 },
        { "name": "office", "id": 21 },
        { "name": "others", "id": 22 },
        { "name": "image_graphics", "id": 23 },
        { "name": "development", "id": 24 },
        { "name": "reading", "id": 25 },
        { "name": "chat", "id": 26 },
        { "name": "tools", "id": 27 },
        { "name": "video", "id": 28 }
    ];

    for (let index = 0; index < categorys.length; index++) {
        const element = categorys[index];
        importDeb(element)
    }
}

export function importDeb(category) {
    return new Promise((resolve, reject) => {
        //
        Vue.axios.get(`http://dcstore.shenmo.tech/store/${category.name}/applist.json`)
            .then(res => {
                var list = res["data"]

                console.log(list)

                for (let index = 0; index < list.length; index++) {
                    let deb = list[index];

                    console.log(deb)

                    lrz(`http://dcstore.shenmo.tech/store/${category.name}/${deb.Pkgname}/icon.png`, {
                        quality: 0.7
                    })
                        .then(function (rst) {
                            let logo = rst.base64

                            let d = deb.Update.match("\\d*年\\d*月\\d*日") ? '2020-07-21 16:11' : deb.Update
                            d = d.match("\\d*\\.\\d*\\.\\d*") ? '2020-07-21 16:11' : d

                            let filename = "FileName" in deb ? deb.FileName : deb.Filename;

                            Vue.axios.post(base + "/deb",
                                {
                                    "deb": {
                                        "name": deb.Name,
                                        "version": deb.Version,
                                        "file_name": filename,
                                        "package_name": deb.Pkgname,
                                        "author": deb.Author,
                                        "uploader": deb.Contributor,
                                        "website": deb.Website,
                                        "update_date": d,
                                        "size": deb.Size,
                                        "desc": deb.More,
                                        "logo": logo,
                                        "screen_shots": "",
                                        "category_id": category.id
                                    }
                                }
                            ).then(res => console.log(res["data"]))
                                .catch(error => console.log(error))

                        })
                        .catch(function (err) {
                            // 处理失败会执行
                        });
                }

                resolve(list)
            })
            .catch(error => reject(error))
    })
}


export function getDebListByUploadDate() {
    return new Promise((resolve, reject) => {
        Vue.axios.get(`${base}/debs/size/8`)
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}

export function getInvalidList() {
    return new Promise((resolve, reject) => {
        Vue.axios.defaults.withCredentials = "include"
        Vue.axios.get(`${base}/debs/invalid`)
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}

export function commitDebInfo(deb) {
    return new Promise((resolve, reject) => {
        Vue.axios.post(`${base}/deb`, {
            "deb": deb
        }, { withCredentials: "include" })
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}

export function getDebDetail(deb_id) {
    return new Promise((resolve, reject) => {
        Vue.axios.get(`${base}/deb/${deb_id}`)
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}

export function searchDeb(keywords) {
    return new Promise((resolve, reject) => {
        Vue.axios.get(`${base}/search`, {
            params: {
                "list": keywords
            }
        })
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}

export function passValidation(deb_id) {
    return new Promise((resolve, reject) => {
        Vue.axios.put(`${base}/deb/validate/${deb_id}`)
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}


export function login(adminForm) {
    return new Promise((resolve, reject) => {

        const md5 = require("md5")
        adminForm["admin_pwd"] = md5(adminForm["admin_pwd"])

        Vue.axios.defaults.withCredentials = "include"
        Vue.axios.post(`${base}/admin/login`, {
            "admin": adminForm
        })
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}


export function regist(adminForm) {
    return new Promise((resolve, reject) => {
        const md5 = require("md5")
        adminForm["admin_pwd"] = md5(adminForm["admin_pwd"])
        Vue.axios.defaults.withCredentials = "include"
        Vue.axios.post(`${base}/admin/regist`, {
            "admin": {
                "admin_name": adminForm.admin_name,
                "admin_pwd": adminForm.admin_pwd,
                "admin_pwd_again": adminForm.admin_pwd
            }
        })
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}

export function checkLogin() {
    return new Promise((resolve, reject) => {
        Vue.axios.defaults.withCredentials = "include"
        Vue.axios.post(`${base}/admin/check`)
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}


export function doDownload(form, action) {
    form.setAttribute("target", "");
    form.setAttribute("method", "get");//设置请求类型  
    form.setAttribute("action", action);//设置请求路径

    form.submit();//表单提交
}


export function downloadDeb(deb_id, form) {
    doDownload(form, base + "/deb_file/" + deb_id);
}

export function logout() {
    return new Promise((resolve, reject) => {
        Vue.axios.defaults.withCredentials = "include"
        Vue.axios.post(`${base}/admin/logout`)
            .then(res => {
                resolve(JSON.parse(res["data"]["msg"]))
            })
            .catch(error => reject(error))
    })
}